package com.laf.manage.service.impl;

import com.laf.common.constant.MQConstant;
import com.laf.common.utils.mq.RabbitMqHelper;
import com.laf.manage.domain.dto.AddItemDocMsg;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class LostItemMessageService {

    private final RabbitMqHelper rabbitMqHelper;

    /**
     * 发送新增失物的消息
     *
     * @param id 失物id
     * @param  status 失物状态
     *           propagation = Propagation.REQUIRES_NEW 创建一个新的事务，不加别的事务
     *           创建新的事务原因是：在发送新增失物消息之前要确保，对mysql 的操作已经完成并且提交到数据库中
     *           在新增的失物才能查询到最新的失物数据
     */
     @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void sendAddLostItemMsg(Long id , Integer status) {
        AddItemDocMsg msg = new AddItemDocMsg(id,status);
        // 发送消息到 mq 中
        rabbitMqHelper.sendMessage(
                MQConstant.LOST_ITEM_EXCHANGE,
                MQConstant.LOST_ITEM_ADD_ROUTING_KEY,
                msg
        );
    }



}
